package com.cocoiko.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class CocoikoAccountStore {

	/**
	 * 
	 */
	public static boolean save(CocoikoAccount account, Context context) {
		Editor editor = context.getSharedPreferences(
				CocoikoConstains.COCOIKO_ACCOUNT, context.MODE_PRIVATE).edit();
		editor.putString(CocoikoConstains.ID, account.getId());
		editor.putString(CocoikoConstains.NAME, account.getName());
		editor.putString(CocoikoConstains.TOKEN, account.getToken());
		editor.putString(CocoikoConstains.GENDER, account.getGender());
		editor.putString(CocoikoConstains.BIRTHDAY, account.getBirthday());
		editor.putString(CocoikoConstains.AVATAR_URL, account.getAvatar_url());
		editor.putString(CocoikoConstains.EMAIL, account.getEmail());
		editor.putString(CocoikoConstains.PASSWORD, account.getPassword());
		editor.putString(CocoikoConstains.INTRODUCE, account.getIntroduce());
		editor.putBoolean(CocoikoConstains.IS_ON_TWITTER, account.isOnTwitter());
		editor.putBoolean(CocoikoConstains.IS_ON_FACEBOOK,
				account.isOnFacebook());
		editor.putBoolean(CocoikoConstains.IS_LEAD_NOTICE,
				account.isLeadNotice());
		editor.putBoolean(CocoikoConstains.IS_FOLLOW_NOTICE,
				account.isFollowNotice());
		editor.putBoolean(CocoikoConstains.IS_LEAD_MAIL, account.isLeadMail());
		editor.putBoolean(CocoikoConstains.IS_FOLLOW_MAIL,
				account.isFollowMail());
		return editor.commit();
	}

	/**
	 * 
	 */
	public static CocoikoAccount restore(Context context) {
		CocoikoAccount account = new CocoikoAccount();
		SharedPreferences prefs = context.getSharedPreferences(
				CocoikoConstains.COCOIKO_ACCOUNT, context.MODE_PRIVATE);
		account.setId(prefs.getString(CocoikoConstains.ID, null));
		account.setToken(prefs.getString(CocoikoConstains.TOKEN, null));
		account.setName(prefs.getString(CocoikoConstains.NAME, null));
		account.setGender(prefs.getString(CocoikoConstains.GENDER, null));
		account.setBirthday(prefs.getString(CocoikoConstains.BIRTHDAY, null));
		account.setAvatar_url(prefs
				.getString(CocoikoConstains.AVATAR_URL, null));
		account.setEmail(prefs.getString(CocoikoConstains.EMAIL, null));
		account.setPassword(prefs.getString(CocoikoConstains.PASSWORD, null));
		account.setIntroduce(prefs.getString(CocoikoConstains.INTRODUCE, null));
		account.setOnTwitter(prefs.getBoolean(CocoikoConstains.IS_ON_TWITTER,
				false));
		account.setOnFacebook(prefs.getBoolean(CocoikoConstains.IS_ON_FACEBOOK,
				false));
		account.setLeadNotice(prefs.getBoolean(CocoikoConstains.IS_LEAD_NOTICE,
				false));
		account.setFollowNotice(prefs.getBoolean(
				CocoikoConstains.IS_FOLLOW_NOTICE, false));
		account.setLeadMail(prefs.getBoolean(CocoikoConstains.IS_LEAD_MAIL,
				false));
		account.setFollowMail(prefs.getBoolean(CocoikoConstains.IS_FOLLOW_MAIL,
				false));
		return account;
	}
	
	/**
	 * 
	 * @param context
	 * @return
	 */
	public static String restoreToken(Context context){
		SharedPreferences prefs = context.getSharedPreferences(
				CocoikoConstains.COCOIKO_ACCOUNT, context.MODE_PRIVATE);
		String token = prefs.getString(CocoikoConstains.TOKEN, null);
		return token;
	}
	/**
	 * 
	 * @param context
	 * @param id
	 * @param token
	 * @return
	 */
	public static boolean saveToken(Context context,String id,String token){
		SharedPreferences prefs = context.getSharedPreferences(
				CocoikoConstains.COCOIKO_ACCOUNT, context.MODE_PRIVATE);
		Editor editor = prefs.edit();
		editor.putString(CocoikoConstains.ID, id);
		editor.putString(CocoikoConstains.TOKEN, token);
		return editor.commit();
	}
}
